################################################################################
## Group Identities and Parliamentary Debates: Replication package
## Fiva, Nedregård and Øien (2025)

# Description:

## Code to make Figure C1

### Figure C1: "Voter preferences measured in surveys: 1981-2017"

################################################################################




# Packages

library(estimatr)
library(grid)
library(data.table)
library(stringr)
library(grid)
library(gridExtra)
library(ggpubr)
library(dplyr)



# Directories (the parent directory is set by master.R)

data.dir <-  "../data/1_raw_data"
fig.dir  <-  "../results/figures"
txt.dir <- "../results/in_text"

# Add montserrat font

library(showtext)
font_add_google(name = "Montserrat", family = "Montserrat")
showtext_auto()

# Data

## The data "SurveyPanel.dta" is confidential. See README.txt for getting
## access to the data.

d <- haven::read_dta(paste(data.dir, "SurveyPanel.dta", sep = "/"))
setDT(d)
d <- d[year > 1980]

words_vector <-  c("barn", "barnehage", "helse", "sykehus", "sjukehus", "samferd", 
                   "eldre", "familie", "skole", "skule", "utdanning", "arbeid",
                   "skatt", "innvandr", "økonomi", "Økonomi", "miljø", "landbruk",
                   "bompeng", "avgift", "kollektiv", "jernbane", "velferd", "olje",
                   "bedrift", "handel", "trygd", "næring", "jordbruk", "militær",
                   "forsvar", "vei", "veg", "utenriks", "utjevning", "fordeling",
                   "sysselsetting", "student", "privat", "pensj", "fiskeri",
                   "gasskraft", "desentr", "distrikt", "industri", "asyl", "norge",
                   "norsk", "kommune", "fylke", "pappaperm", "samfunn", "prisstig",
                   "pristig", "inflasj", "rente", "zeuz", "abort", "nato", "eec",
                   "eøs", "atom")


# Iterate over each word in the words_vector
for (word in words_vector) {
  
  # Create a new column name based on the word
  new_col_name <- paste0("m", word)
  
  # Use set to create a new column in the dataset
  # The new column will be 1 if the word is in important1 or important2, and 0 otherwise
  set(d, j = new_col_name, value = as.integer(str_detect(d[["important1"]], regex(word, ignore_case = F)) | 
                                                      str_detect(d[["important2"]], regex(word, ignore_case = F))))
}



d[, `:=` (nuclear         = case_when(matom == 1 ~ 1, TRUE ~ 0),
          EU              = case_when(mzeuz == 1 | meec == 1 | meøs == 1 ~ 1, TRUE ~ 0),
          NATO            = case_when(mnato == 1 ~ 1, TRUE ~ 0),
          Norway          = case_when(mnorsk == 1 | mnorge == 1 ~ 1, TRUE ~ 0),
          industry        = case_when(mindustri == 1 ~ 1, TRUE ~ 0),
          regionalpolicy  = case_when(mdesentr == 1 | mdistrikt == 1 ~ 1, TRUE ~ 0),
          foreign         = case_when(mutenriks == 1 ~ 1, TRUE ~ 0),
          redistribution  = case_when(mutjevning == 1 | mfordeling == 1 ~ 1, TRUE ~ 0),
          agriculture     = case_when(mlandbruk==1|mjordbruk==1|mfiskeri==1 ~ 1, TRUE ~ 0),
          defense         = case_when(mmilitær==1|mforsvar==1 ~ 1, TRUE ~ 0),
          taxation        = case_when(mskatt==1 | mavgift==1 | mbompeng==1 ~ 1, TRUE ~ 0),
          education       = case_when(mutdanning==1 | mskole==1 | mskule==1 | mstudent==1 ~ 1, TRUE ~ 0),
          healthcare      = case_when(mhelse==1|msykehus==1|msjukehus==1 ~ 1, TRUE ~ 0),
          family          = case_when(mfamilie==1 ~ 1, TRUE ~ 0),
          environment     = case_when(mmiljø==1 ~ 1, TRUE ~ 0),
          children        = case_when(mbarn==1 ~ 1, TRUE ~ 0),
          childcare       = case_when(mbarnehage==1 ~ 1, TRUE ~ 0),
          pension         = case_when(mpensj==1 ~ 1, TRUE ~ 0),
          immigration     = case_when(minnvandr==1|masyl==1 ~ 1, TRUE ~ 0),
          economy         = case_when(møkonomi==1|mØkonomi==1 ~ 1, TRUE ~ 0),
          inflation       = case_when(mprisstig==1|mpristig==1|minflasj==1 ~ 1, TRUE ~ 0),
          transport       = case_when(msamferd==1|mkollektiv==1|mjernbane==1|mvei==1|mveg==1 ~ 1, TRUE ~ 0),
          welfare         = case_when(mvelferd==1|mtrygd==1 ~ 1, TRUE ~ 0),
          employment      = case_when(marbeid==1|msysselsetting==1 ~ 1, TRUE ~ 0),
          oil             = case_when(molje==1|mgasskraft==1 ~ 1, TRUE ~ 0),
          abortion        = case_when(mabort==1 ~ 1, TRUE ~ 0),
          eldercare       = case_when(meldre==1 ~ 1, TRUE ~ 0),
          business        = case_when(mbedrift==1|mhandel==1|mnæring==1 ~ 1, TRUE ~ 0))]




# MODELS



cvar <- c("EU", "abortion", "transport", "taxation", 
          "economy", "education", "children", "eldercare", "family", "healthcare", 
          "environment", "immigration", "welfare", "employment", "business", 
          "agriculture", "redistribution", "pension", "regionalpolicy", 
          "Norway", "defense")


## Left/Right

f_left <- formula(glue::glue("left ~ {paste(cvar, collapse = \"+\")}"))

m_left <- lm_robust(formula = f_left, data = d, se_type = "HC1")

m_data_left <- tidy(m_left) %>% 
  mutate(dep.var = "Left-wing") %>%
  arrange(-estimate) %>%
  filter(term != "(Intercept)")

## Writing coefficient values for the most positive and negative effect on 
## likelihood of left

beta_redistribution <- m_data_left %>% filter(term == "redistribution") %>% select(estimate) %>% pull

writeLines(sprintf("%.0f", beta_redistribution*100), 
           paste(txt.dir, "beta_redistribution.txt", sep = "/"))


beta_business <- m_data_left %>% filter(term == "business") %>% select(estimate) %>% pull

writeLines(sprintf("%.0f", -beta_business*100), 
           paste(txt.dir, "beta_business.txt", sep = "/"))

write(sprintf("$%.2f$", m_left$r.squared), 
      file = paste(txt.dir, "r_squared_bloc.txt", sep = "/"), append = FALSE)


## male/female

f_male <- formula(glue::glue("male ~ {paste(cvar, collapse = \"+\")}"))

m_male <- lm_robust(formula = f_male, data = d, se_type = "HC1")

m_data_male <- tidy(m_male) %>% mutate(dep.var = "Men") %>%
  arrange(-estimate) %>%
  filter(term != "(Intercept)")

writeLines(sprintf("%.2f",m_male$r.squared), 
           paste(txt.dir, "r_squared_men.txt", sep = "/"))


## young

f_young <- formula(glue::glue("young ~ {paste(cvar, collapse = \"+\")}"))

m_young <- lm_robust(formula = f_young, data = d, se_type = "HC1")

writeLines(sprintf("%.2f",m_young$r.squared), 
           paste(txt.dir, "r_squared_young.txt", sep = "/"), sep = "")

m_data_young <- tidy(m_young) %>% mutate(dep.var = "Young") %>%
  arrange(-estimate) %>%
  filter(term != "(Intercept)")


## rural

f_rural <- formula(glue::glue("rural ~ {paste(cvar, collapse = \"+\")}"))

m_rural <- lm_robust(formula = f_rural, data = d, se_type = "HC1")

writeLines(sprintf("%.2f",m_rural$r.squared), 
           paste(txt.dir, "r_squared_rural.txt", sep = "/"))

m_data_rural <- tidy(m_rural) %>% mutate(dep.var = "Rural") %>%
  arrange(-estimate) %>%
  filter(term != "(Intercept)")

# PLOTTING

## FUNCTION

p_function <- function(data, title){
  
  ggplot(data, aes(x = estimate, y = factor(term, levels = term))) +
    geom_point(size = 2) +
    geom_segment(aes(x = conf.low, xend = conf.high, yend = term)) +
    
    
    theme(
      panel.grid = element_blank(),
      panel.border = element_blank(),
      panel.grid.major.y= element_line(linewidth = 0.2, colour = "grey90"),
      panel.background = element_rect(fill = "transparent"),
      plot.background = element_rect(fill = "transparent"),
      plot.title = element_text(family = "Montserrat", color = "black", size = 20, hjust = 0.5, margin=margin(0,0,10,0),),
      panel.grid.major.x = element_blank(),
      strip.text = element_text(family = "Montserrat", color = "black", size = 14),
      strip.background = element_blank(),
      axis.ticks.length = unit(0, "lines"),
      axis.text.y = element_text(family = "Montserrat", size = 10),
      axis.title.y = element_text(family = "Montserrat", size = 13, margin=margin(0,0,0,0)),
      axis.text.x = element_text(family = "Montserrat", size = 10),
      axis.title.x = element_text(family = "Montserrat", size = 13, margin=margin(20,0,0,0))
    ) +
    scale_x_continuous(breaks = seq(-0.5,0.5,0.1), limits = c(-.5,.5)) +
    labs(x = "Coefficient", y = "", title = title)
  
}

font_add_google(name = "Montserrat", family = "Montserrat")
showtext_auto()


## LEFT-WING

p_left <- p_function(m_data_left, title = "Left-wing")

## Men

p_men <- p_function(m_data_male, title = "Men")

## Young

p_young <- p_function(m_data_young, title = "Young")

## Rural

p_rural <- p_function(m_data_rural, title = "Rural")

p.figure.2 <- grid.arrange(p_left, p_men, p_young, p_rural)



ggsave(plot = p.figure.2, file = paste(fig.dir, "figC1.pdf", sep = "/"),
       width = 9, height = 9, pointsize = 12, device = cairo_pdf)



